var eventVm = new Vue({
	el: "#demand-record-wrapper",
	data: function() {
		return {
			depart: '',
			departid: '',
			departError: '',
			workshop: '',
			workshopid: '',
			workshopError: '',
			demandType: '', // 需求类别
			demandTypeError: '',
			demandTypeCode: '',
			demandProcess: '', // 需求过程
			demandProcessError: '',
			demandProcessCode: '',
			corSystems: '', // 关联系统
			corSystemsError: '',
			corSystemsCode: '',
			demandLevel: '低',
			demandLevelError: '',
			demander: '',
			demanderError: '',
			phoneNumber: '',
			phoneNumberError: '',
			email:'',
			emailError: '',
			handler: '',
			handlerError: '',
			handlerCode: '',
			demandContent: '',
			demandContentError: '',
			solution: '',
			solutionError: ''
		}
	},
	mounted: function (){
		var self = this;
		mui.init();
		mui.plusReady(function () {
		    self.initLayout();
			self.initDemandType();
			self.initDemandProcess();
			self.initCorSystemsData();
			self.initValidator();
			self.initDapertData();
			self.initWorkshopData();
			self.initHandlerData();
		})
	},
	methods:{
		initLayout:function() {
			mui('.mui-scroll-wrapper').scroll({
				scrollY: true, //是否竖向滚动
				scrollX: false, //是否横向滚动
				startX: 0, //初始化时滚动至x
				startY: 0, //初始化时滚动至y
				indicators: true, //是否显示滚动条
				deceleration: 0.0006, //阻尼系数,系数越小滑动越灵敏
				bounce: true //是否启用回弹
			})
		},
		initDemandType: function() {
			var self = this;
			var url = config.requestBaseUrl  + "getSystemStandData";
			this.demandPicker = new mui.PopPicker();
			//console.log("测试")
			mui.ajax(url,{
				data:{
					type: "demandType"
				},
				dataType:'json',//服务器返回json格式数据
				type:'get',//HTTP请求类型
				timeout:3000,//超时时间设置为10秒；
				success:function(data){
					if(data.isSuccess) {
						var pickerData = [];
						data.data.forEach(function(item,index) {
							pickerData.push({text: item.name,value:item.id});
						})
						self.demandPicker.setData(pickerData);
					}
				},
				error:function(xhr,type,errorThrown){
					console.log("失败")
				}
			});
		},
		initDemandProcess: function() {
			var self = this;
			var url = config.requestBaseUrl  + "getSystemStandData";
			this.demandProcessPicker = new mui.PopPicker();
			//console.log("测试")
			mui.ajax(url,{
				data:{
					type: 'process'
				},
				dataType:'json',//服务器返回json格式数据
				type:'get',//HTTP请求类型
				timeout:3000,//超时时间设置为10秒；
				success:function(data){
					if(data.isSuccess) {
						var pickerData = [];
						data.data.forEach(function(item,index) {
							pickerData.push({text: item.name,value:item.id});
						})
						self.demandProcessPicker.setData(pickerData);
					}
				},
				error:function(xhr,type,errorThrown){
					console.log("失败")
				}
			});
		},
		initCorSystemsData: function() {
			var self = this;
			var url = config.requestBaseUrl  + "getSystemStandData";
			this.corSystemsPicker = new mui.PopPicker();
			//console.log("测试")
			mui.ajax(url,{
				data:{
					type: 'software'
				},
				dataType:'json',//服务器返回json格式数据
				type:'get',//HTTP请求类型
				timeout:3000,//超时时间设置为10秒；
				success:function(data){
					if(data.isSuccess) {
						var pickerData = [];
						data.data.forEach(function(item,index) {
							pickerData.push({text: item.name,value:item.id});
						})
						self.corSystemsPicker.setData(pickerData);
					}
				},
				error:function(xhr,type,errorThrown){
					console.log("失败")
				}
			});
		},
		initDapertData: function() {
			var self = this;
			this.departPicker = new mui.PopPicker();
			var url = config.requestBaseUrl + 'getSystemStandData';
			mui.ajax(url,{
			 	data:{
					type: 'depart'
			 	},
			 	dataType:'json',//服务器返回json格式数据
			 	type:'get',//HTTP请求类型
			 	timeout:3000,//超时时间设置为10秒；
			 	success:function(data){
					var departData = [];
					if(data.isSuccess) {
						data.data.forEach(function(item, index) {
							departData.push({value: item.id, text: item.name})
						})
						self.departPicker.setData(departData);
					}
			 	},
			 	error:function(xhr,type,errorThrown){
					mui.alert("出错了:" + errorThrown);
			 	}
			});
		},
		initWorkshopData: function() {
			var self = this;
			this.workshopPicker = new mui.PopPicker();
			var url = config.requestBaseUrl + 'getSystemStandData';
			mui.ajax(url,{
			 	data:{
					type: 'workshop'
			 	},
			 	dataType:'json',//服务器返回json格式数据
			 	type:'get',//HTTP请求类型
			 	timeout:3000,//超时时间设置为10秒；
			 	success:function(data){
					self.workshopSourceData = data.data;
			 	},
			 	error:function(xhr,type,errorThrown){
					mui.alert("出错了:" + errorThrown);
			 	}
			});
		},
		initHandlerData: function() {
			var self = this;
			this.handlerPicker = new mui.PopPicker();
			var url = config.requestBaseUrl + 'getSystemStandData';
			mui.ajax(url,{
			 	data:{
					type: 'demandHandler'
			 	},
			 	dataType:'json',//服务器返回json格式数据
			 	type:'get',//HTTP请求类型
			 	timeout:3000,//超时时间设置为10秒；
			 	success:function(data){
					var pickerData = [];
					if(data.isSuccess) {
						data.data.forEach(function(item,index) {
							pickerData.push({text: item.name,value:item.id});
						})
						self.handlerPicker.setData(pickerData);
					}
			 	},
			 	error:function(xhr,type,errorThrown){
					mui.alert("出错了:" + errorThrown);
			 	}
			});
		},
		initValidator: function() {
			this.constraints = {
				depart: {
					presence: true
				},
				demandType: {
					presence: true
				},
				demandProcess: {
					presence: true
				},
				corSystems: {
					length: {
						maximum: 20,
						message: "长度不能超过20"
					}
				},
				demandLevel: {
					presence: true
				},
				demander: {
					presence: true,
					length: {
						maximum: 10,
						message: "长度不能超过10"
					}
				},
				phoneNumber: {
					
				},
				email: {
				},
				handler: {
					presence: true
				},
				demandContent: {
					presence: true,
					length: {
						maximum: 60,
						message: "长度不能超过60"
					}
				},
				solution: {
					length: {
						maximum: 60,
						message: "长度不能超过60"
					}
				}
			};
			// 为空时的信息
			validate.validators.presence.message = "不能为空!";
		},
		toDepartPicker: function() {
			var self = this;
			this.clearErrorInfo('depart');
			this.departPicker.show(function (selectItems) {
				self.depart = selectItems[0].text;
				self.departid = selectItems[0].value;
				setTimeout(function() {
					self.workshopDataConst();
				}, 10);
			})
		},
		toWorkshopPicker:function() {
			var self = this;
			this.clearErrorInfo('workshop');
			if(!this.depart) {
				mui.toast("请先选择部门!")
			} else {
				this.workshopPicker.show(function (selectItems) {
					self.workshop = selectItems[0].text;
					self.workshopid = selectItems[0].value;
				})	
			}
		},
		toDemandTypePicker: function() {
			var self = this;
			this.clearErrorInfo('demandType');
			this.demandPicker.show(function (selectItems) {
				self.demandType = selectItems[0].text;
				self.demandTypeCode = selectItems[0].value;
			});
		},
		toDemandProcessPicker: function() {
			
			var self = this;
			this.clearErrorInfo('demandProcess');
			this.demandProcessPicker.show(function (selectItems) {
				self.demandProcess = selectItems[0].text;
				self.demandProcessCode = selectItems[0].value;
			});
		},
		toCorSystemsPicker: function() {
			var self = this;
			this.clearErrorInfo('corSystems');
			this.corSystemsPicker.show(function (selectItems) {
				self.corSystems = selectItems[0].text;
				self.corSystemsCode = selectItems[0].value;
			});
		},
		toHandlerPicker: function() {
			/**
			 * 处理人, 在用户表中获取有需求处理权限的用户
			 */
			var self = this;
			this.clearErrorInfo('handler');
			this.handlerPicker.show(function (selectItems) {
				self.handler = selectItems[0].text;
				self.handlerCode = selectItems[0].value;
			});
			
		},
		workshopDataConst: function() {
			var self = this;
			var workshopData = [];
			this.workshopSourceData.forEach(function(item, index) {
				if(item.id == self.departid) {
					workshopData.push({value: item.id, text: item.name});
				}
			})
			this.workshopPicker.setData(workshopData);
		},
		radioSelected: function(value) {
			this.demandLevel = value;
			console.log(value);
		},
		clearErrorInfo: function(filed) {
			this[ filed+ 'Error'] = '';
		},
		submitDemand: function() {
			var data = {};
			var self = this;
			if(this.depart) {
				data['depart'] = this.depart;
			}
			if(this.demandType) {
				data['demandType'] = this.demandType;
			}
			if(this.demandType) {
				data['demandProcess'] = this.demandProcess;
			}
			if(this.corSystems) {
				data['corSystems'] = this.corSystems;
			}
			if(this.demandLevel) {
				data['demandLevel'] = this.demandLevel;
			}
			if(this.demander) {
				data['demander'] = this.demander;
			}
			if(this.phoneNumber) {
				data['phoneNumber'] = this.phoneNumber;
			}
			if(this.demandContent) {
				data['demandContent'] = this.demandContent;
			}
			if(this.solution) {
				data['solution'] = this.solution;
			}
			if(this.handler) {
				data['handler'] = this.handler;
			}
			var errors = validate(data, this.constraints);
			for(x in errors) {
				console.log(x + ":" + errors[x]) 
			}
			console.log(errors)
			if(errors) {
				if(errors.depart) {
					this.departError = errors.depart;
				}
				if(errors.demandType) {
					this.demandTypeError = errors.demandType;
				}
				if(errors.demandProcess) {
					this.demandProcessError = errors.demandProcess;
				}
				if(errors.corSystems) {
					this.corSystemsError = errors.corSystems;
				}
				if(errors.demandLevel) {
					this.demandLevelError = errors.demandLevel;
				}
				if(errors.demander) {
					this.demanderError = errors.demander;
				}
				if(errors.phoneNumber) {
					this.phoneNumberError = errors.phoneNumber;
				}
				if(errors.demandContent) {
					this.demandContentError = errors.demandContent;
				}
				if(errors.solution) {
					this.solutionError = errors.solution;
				}
				if(errors.handler) {
					this.handlerError = errors.handler;
				}
			} else {
				this.submitData();
			}
		},
		submitData: function() {
			var url = config.requestBaseUrl + 'addDemand';
			var recorderid = plus.storage.getItem("userid");
			console.log(this.demandTypeCode)
			console.log(this.demandProcessCode)
			console.log(this.corSystemsCode)
			console.log(this.handlerid)
			mui.ajax(url,{
			 	data:{
			 		typeCode: this.demandTypeCode,
					processCode: this.demandProcessCode,
					corSystemsCode: this.corSystemsCode,
					level: this.demandLevel,
					content: this.demandContent,
					demander: this.demander,
					phone: this.phoneNumber,
					email: this.email,
					solution: this.solution,
					handlerid: this.handlerCode,
					recorderid: recorderid,
					depart: this.depart,
					departid: this.departid,
					workshop: this.workshop,
					workshopid: this.workshopid
			 	},
			 	dataType:'json',//服务器返回json格式数据
			 	type:'post',//HTTP请求类型
			 	timeout:5000,//超时时间设置为10秒；
			 	success:function(data){
			 		mui.toast(data.msg);
					if(data.isSuccess) {
						var thisPage = plus.webview.currentWebview();
						thisPage.close();
					}
			 	},
			 	error:function(xhr,type,errorThrown){
			 		mui.toast("失败!!!");
			 	}
			 });
		 }
	}
})